package com.hjx.windowtool.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import lombok.extern.slf4j.Slf4j;

/**
 * 第八区
 * @author HuangJianxiong
 * 2020年5月25日 上午10:31:32
 */
@Slf4j
public class Xiaoshuo2ksDownloadTest2 {
	private static final ExecutorService executorService = Executors.newFixedThreadPool(10);
	private static String info;
	
	public static void main(String[] args) throws Exception {
		
		String s="01232阿达萨达";
		boolean matches = s.matches(".*[0-9]+.*");
		System.out.println(matches);
				
		
		String dir = "E:\\book\\12";
		String fileName = "网游进化一切";
		String url = "http://m.2kxs.org/139_139975/";
		
		download(dir, fileName, url);
		
	}


	/**
	 * @param dir
	 * @param fileName
	 * @param url
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	public static void download(String dir, String fileName, String url) throws IOException, UnsupportedEncodingException {
		getContentUrls(dir, url);
		loadBookContentDibaqu(dir);
		packageFile(dir, fileName);
	}


	/**
	 * @param dir
	 * @param url
	 * @return
	 * @throws IOException
	 */
	public static List<String> getContentUrls(String dir, String url) throws IOException {
		if (!new File(dir).exists() || !new File(dir).isDirectory()) {
			new File(dir).mkdirs();
		}
		Connection conn = Jsoup.connect(url);
		 conn.header("User-Agent", XiaoShuoTest2.userAgent);
		 conn.header("Referer", "http://m.2kxs.org");
		 Document d = conn.get();
		 System.out.println(d);
		 info = d.select(".info2").text();
		 
		 Element element = d.select(".list-charts").get(0);
		 System.out.println(element);
//		 Element element = d.select("#list-chapterAll").get(0);
		 Elements select = element.select("a");
		 List<String> listUrl=new ArrayList<>();
		 List<String> listTitle=new ArrayList<>();
		 for (Element e : select) {
//		 System.out.println(e.attr("abs:href")+"==="+e.text());
    		 listUrl.add(e.attr("abs:href"));
    		 listTitle.add(e.text());
		 }
		 Files.write(Paths.get(dir+"//url.text"), listUrl);
		 Files.write(Paths.get(dir+"//titel.text"), listTitle);
		 log.info("获取到{}章",listTitle.size());
		return listTitle;
	}


	/**
	 * @param dir
	 * @param fileName
	 * @param listTitle
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	public static void packageFile(String dir, String fileName) throws IOException, UnsupportedEncodingException {
		List<String> listTitle=Files.readAllLines(new File(dir,"titel.text").toPath());
		if (Files.exists(new File(dir,listTitle.size()-1+".txt").toPath())) {
			List<String> list = Files.readAllLines(new File(dir,"titel.text").toPath());
			try(BufferedWriter bw=new BufferedWriter(new FileWriter(new File(dir,fileName+".txt")))){
				bw.write(info);
				bw.newLine();
				int num=1;
				for (int i = 0; i < list.size(); i++) {
					String string = list.get(i);
					if(!new String(Files.readAllBytes(new File(dir,i+".txt").toPath())).contains(string.trim().split(" ")[0])) {
						if (string.contains("章")||string.contains("节")) {
							
						}else if(string.matches(".*[0-9]+.*")) {
							bw.write((num++)+"章");
						}
						bw.write(string);
						bw.newLine();
					}
					List<String> readAllLines = Files.readAllLines(new File(dir,i+".txt").toPath());
					for (String string2 : readAllLines) {
						if (string2==null||string2.trim().isEmpty()) {
							continue;
						}
						bw.write(string2);
						bw.newLine();
					}
				}
			}
			
		} else {
			XiaoShuoTest2.packageChm(dir, fileName);
		}
	}


	/**
	 * @param dir
	 * @throws IOException
	 */
	public static void loadBookContentDibaqu(final String dir) throws IOException {
		final List<String> listUrl = Files.readAllLines(Paths.get(dir+"//url.text"));
		for (int j = 0; j < listUrl.size(); j++) {
			final int i=j;
			Runnable runnable = new Runnable() {
				public void run() {
					while (true) {
						String url = listUrl.get(i);
						try {
							log.info("获取第{}条数据：{}", i, url);
							Connection conn = Jsoup.connect(url);
							conn.header("User-Agent", XiaoShuoTest2.userAgent);
							Document d = conn.get();
//							System.out.println(d);
							Elements select = d.select(".imagecontent");
							if (select.size() > 0) {
								String attr = select.attr("src");
								log.info("获取到图片：{}", attr);
								XiaoShuoTest2.downloadImage(attr, dir + "//" + i + ".gif", url);
							} else {
								List<String> list = new ArrayList<>(Arrays.asList(d.select(".content-ext").html().split("<br>")));
								System.out.println(d.select(".next").text());
								if("下一页".equals(d.select(".next").text().trim())) {
									String href = d.select(".next").select("a").attr("abs:href");
									if (!href.startsWith("http")) {
										href="http://m.2kxs.org/"+href;
									}
									d = conn.url(href).get();
//									System.out.println(d);
									list.addAll(Arrays.asList(d.select(".content-ext").html().split("<br>")));
								}
								if("下一页".equals(d.select(".next").text().trim())) {
									String href = d.select(".next").select("a").attr("abs:href");
									if (!href.startsWith("http")) {
										href="http://m.2kxs.org/"+href;
									}
									d = conn.url(href).get();
//									System.out.println(d);
									list.addAll(Arrays.asList(d.select(".content-ext").html().split("<br>")));
								}
								if (list.size() >= 2) {
									list.remove(list.size() - 1);
								}
								if (list.size() > 0) {
									list.remove(0);
								}
								List<String> listContents = new ArrayList<>();
								for (String string : list) {
									if(string.contains("下一页")) {
										continue;
									}
									listContents.add(string.replaceAll("&nbsp;", " "));
								}
								Files.write(Paths.get(dir + "//" + i + ".txt"), listContents);
								log.info("{}保存文本成功", i);
							}
							break;
						} catch (Exception e) {
							log.error("获取：{}-{}数据失败：{}", i, url, e);
						}finally {
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								
							}
						}
					}
				}
			};
			executorService.submit(runnable);
		}
		executorService.shutdown();
		while (true) {
			if (executorService.isTerminated()) {
				log.info("数据下载完成");
				break;
			}
		}
	}
	

	static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
		public java.security.cert.X509Certificate[] getAcceptedIssuers() {
			return null;
		}

		public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
			return true;
		}

		public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
			return true;
		}

		public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException {
			return;
		}

		public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException {
			return;
		}
	}
}
